package com.roger.myHome.util;


import com.google.common.base.Charsets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String dateStrAdd2(String date, int num){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date da = df.parse(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(da);
            calendar.add(Calendar.DAY_OF_MONTH, num);
            return df.format(calendar.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String today(){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date da = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(da);
            return df.format(calendar.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static Integer currentYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return Integer.valueOf(format.format(new Date()));
    }

    public static String dateToString(Date da, String format){
        DateFormat df = new SimpleDateFormat(format);
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(da);
            return df.format(calendar.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String currentMonth(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getCurrentTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

    public static Date strToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Integer[] formatDateString(String d){
        Integer[] r = new Integer[3];
        String[] s = d.substring(0,10).trim().split("-");
        for (Integer i = 0; i < s.length; i++) {
            r[i] = Integer.valueOf(s[i]);
        }
        return r;
    }

    public static Long getDiffDays(String max,String min) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(max);
        Date date1 = sdf.parse(min);
        return (date.getTime() - date1.getTime())/(60*60*24*1000);
    }

    public static void main(String[] args) throws Exception{
        //System.out.println(getDiffDays("2024-07-31 12:00:00", "2024-07-30 11:00:00"));
        currentYear();
    }


}
